package com.bolt.admin.module.sys.service.converter;


import com.bolt.admin.module.sys.dto.MenuDTO;
import com.bolt.admin.module.sys.entity.MenuEntity;
import com.bolt.common.reflect.ObjectUtil;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class MenuConverter {
    private MenuConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param menu DO对象
     * @return DTO对象
     */
    public static MenuDTO toDTO(MenuEntity menu) {
        if (Objects.isNull(menu)) {
            return null;
        }
        MenuDTO result = new MenuDTO();
        result.setId(menu.getId());
        result.setTitle(menu.getTitle());
        result.setUrl(menu.getUrl());
        result.setIcon(menu.getIcon());
        result.setPid((Long) menu.getPid());
        result.setOrderRank(menu.getOrderRank());
        result.setDescription(menu.getDescription());
        result.setEnabled(menu.getEnabled());
        result.setChildrenSize(menu.getChildrenSize());
        result.setLevel(menu.getLevel());
        result.setComponent(menu.getComponent());
        result.setComponentName(menu.getComponentName());
        result.setHidden(menu.getHidden());
        result.setIFrame(menu.getIFrame());
        result.setCache(menu.getCache());
        result.setCreateBy(menu.getCreateBy());
        result.setUpdateBy(menu.getUpdateBy());
        result.setCreateDate(menu.getCreateDate());
        result.setUpdateDate(menu.getUpdateDate());
        result.setVersionNO(menu.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param menu DO对象
     * @return VO对象
     */
    public static MenuEntity toEntity(MenuDTO menu) {
        if (Objects.isNull(menu)) {
            return null;
        }
        MenuEntity result = new MenuEntity();
        result.setId(menu.getId());
        result.setTitle(menu.getTitle());
        result.setUrl(menu.getUrl());
        result.setIcon(menu.getIcon());
        if (ObjectUtil.isNotNull(menu.getPid()) && menu.getPid().longValue() != 0L) {
            result.setParent(new MenuEntity(menu.getPid()));
        }
        result.setOrderRank(menu.getOrderRank());
        result.setDescription(menu.getDescription());

        result.setEnabled(menu.getEnabled());
        result.setCreateBy(menu.getCreateBy());
        result.setUpdateBy(menu.getUpdateBy());
        result.setCreateDate(menu.getCreateDate());
        result.setUpdateDate(menu.getUpdateDate());

        result.setComponent(menu.getComponent());
        result.setComponentName(menu.getComponentName());
        result.setHidden(menu.getHidden());
        result.setIFrame(menu.getIFrame());
        result.setCache(menu.getCache());
        result.setVersionNO(menu.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param menu DO对象
     * @return VO对象
     */
    public static MenuEntity toEntity(MenuEntity result, MenuDTO menu) {
        if (Objects.isNull(menu)) {
            return null;
        }

        result.setTitle(menu.getTitle());
        result.setUrl(menu.getUrl());
        result.setIcon(menu.getIcon());
        result.setPid(menu.getPid());
        result.setOrderRank(menu.getOrderRank());
        result.setDescription(menu.getDescription());
        result.setEnabled(menu.getEnabled());
        result.setChildrenSize(menu.getChildrenSize());
        result.setLevel(menu.getLevel());
        result.setComponent(menu.getComponent());
        result.setComponentName(menu.getComponentName());
        result.setHidden(menu.getHidden());
        result.setIFrame(menu.getIFrame());
        result.setCache(menu.getCache());
        result.setCreateBy(menu.getCreateBy());
        result.setUpdateBy(menu.getUpdateBy());
        result.setCreateDate(menu.getCreateDate());
        result.setUpdateDate(menu.getUpdateDate());
        result.setVersionNO(menu.getVersionNO());
// TODO
        return result;
    }

    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param menuList Entity集合对象
     * @return DTO对象
     */
    public static List <MenuDTO> toDTO(List <MenuEntity> menuList) {
        if (Objects.isNull(menuList)) {
            return null;
        }
        return menuList.stream().map(MenuConverter::toDTO).collect(Collectors.toList());
    }

}
